<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\common\controller\Api;
use think\Exception;

/**
 * 充值接口
 */
class Recharge extends Api
{
    protected $noNeedLogin = ['epay'];
    protected $noNeedRight = ['*'];

    protected $config;

    /**
     * 充值套餐
     *
     */
    public function recharge()
    {
        $list = \app\admin\model\recharge\Recharge::order('face_value asc')->select();

        $this->success('请求成功',$list);
    }

    //配置
    public function config()
    {
        $this->config = get_addon_config('recharge');

        $this->success(__(''), $this->config);
    }


    /**
     * 创建订单并发起支付请求
     */
    public function submit()
    {
        $info = get_addon_info('epay');
        if (!$info || !$info['state']) {
            $this->error('请在后台插件管理安装微信支付宝整合插件后重试');
        }
        $recharge_id = $this->request->param('recharge_id/f');
        $money = $this->request->param('money/f');
        $paytype = $this->request->param('paytype', 'wechat');
        $returnurl = $this->request->param('returnurl', '', 'trim');
        //$openid = $this->request->param('openid', '', 'trim');
        if ($recharge_id == 0 && $money == 0) {
            $this->error('缺少必填参数商品数据');
        }
        if ($money <= 0) {
            $this->error('充值金额不正确');
        }
        if (isset($this->config['minmoney']) && $money < $this->config['minmoney']) {
            $this->error('充值金额不能低于' . $this->config['minmoney'] . '元');
        }
        $user = \app\common\model\User::get($this->auth->id);

        $openid = $user['wechat_open_id'];

        try {
            $response = \addons\recharge\library\Order::submit($money, $paytype, 'miniapp', $openid, '', $returnurl,$recharge_id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success(__(''), $response);
    }


    /**
     * 企业支付通知和回调
     */
    public function epay()
    {
        $type = $this->request->param('type');
        $paytype = $this->request->param('paytype');
        if ($type == 'notify') {
            $pay = Service::checkNotify($paytype);
            if (!$pay) {
                echo '签名错误';
                return;
            }
            //判断是V2还是V3
            $data = Service::isVersionV3() ? $pay->callback() : $pay->verify();

            try {
                //微信支付V3返回和V2不同
                if (Service::isVersionV3() && $paytype === 'wechat') {
                    $data = $data['resource']['ciphertext'];
                    $data['total_fee'] = $data['amount']['total'];
                }

                $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
                \addons\recharge\library\Order::settle($data['out_trade_no'], $payamount);
            } catch (Exception $e) {
                \think\Log::record("回调逻辑处理错误:" . $e->getMessage(), "error");
            }

            //下面这句必须要执行,且在此之前不能有任何输出
            if (Service::isVersionV3()) {
                return $pay->success()->getBody()->getContents();
            } else {
                return $pay->success()->send();
            }

        } else {
            $this->success("请返回网站查看支付状态!", url("user/index"));
        }
    }
}
